package SV_XPATH;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.CompiledExpression;

public class Example extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse) throws ServletException, IOException {
        super.doGet(httpServletRequest, httpServletResponse);
        String userName = httpServletRequest.getParameter("user.name");
        String userDescr = httpServletRequest.getParameter("user.descr");
        String xmlData = "<users><user><name>" + userName + "</name><account>123456</account></user></users>";
        JXPathContext context = JXPathContext.newContext(xmlData);
        CompiledExpression compiledExpression = JXPathContext.compile("string(//user[name/text()='" + userName + "']/account/text())");
        compiledExpression.setValue(context, userDescr);
        httpServletResponse.getOutputStream().println("User info updated");
    }
}
